//
//  TTAPIRequest.swift
//  TTProduct
//
//  Created by Toj on 1/14/21.
//

import Foundation

struct TTAPIRequest {
    
    struct Configuration {
        static var appKey: String = "1b5332a95b4943df9d83dbd1f3dd3c9f"
    }
    
    struct Host {
        var rawValue: String
    }
    
    struct API {
        struct Common {
            var rawValue: String
        }
        
        struct Service {
            var rawValue: String
        }
        
        var rawValue: String
    }
    
    struct Header {
        var rawValue: [String:String]
    }
    
    struct Parameters { }
}

// 域名
extension TTAPIRequest.Host {
    func appending(string: String) -> TTAPIRequest.Host {
        return TTAPIRequest.Host(rawValue: rawValue + string)
    }
    
    func appendingPath(string: String) -> TTAPIRequest.Host {
        return TTAPIRequest.Host(rawValue: rawValue.appending(string))
    }
}

// 域名
extension TTAPIRequest.Host {
    #if DEBUG
    static var api: TTAPIRequest.Host = TTAPIRequest.Host(rawValue: "http://api-test.vipcode.com")
    #else
    static var api: TTAPIRequest.Host = TTAPIRequest.Host(rawValue: "http://api.vipcode.com")
    #endif
}

extension TTAPIRequest.API.Common {
    static var api: TTAPIRequest.API.Common = TTAPIRequest.API.Common(rawValue: "/api")
}

extension TTAPIRequest.API.Service: TTAPIRequestServiceProtocol {
    var value: String {
        return rawValue
    }
}

extension TTAPIRequest.API : Hashable, Equatable, RawRepresentable {
    
    static var base: String = TTAPIRequest.Host.api.appendingPath(string: Common.api.rawValue).rawValue
}

extension TTAPIRequest.Header: TTPersistence {
    
    static var secret: TTAPIRequest.Header =
        TTAPIRequest.Header(rawValue:
                                ["Content-Type": "application/json",
                                 "appKey": TTAPIRequest.Configuration.appKey])
    
    static var general: TTAPIRequest.Header =
        TTAPIRequest.Header(rawValue:
                                ["Content-Type" : "application/json"]).fixHeaders()
    
    private func fixHeaders() -> TTAPIRequest.Header {
        var header = rawValue
        header["token"] = token
        header["secret"] = secret
        
        #if canImport(AppKit)
        header["os"] = "2"
        header["version"] = "1.0.0"
        #endif
        
        #if canImport(UIKit)
        header["os"] = "3"
        header["version"] = "1.0.0"
        #endif
        
        return TTAPIRequest.Header(rawValue: header)
    }
}

extension TTAPIRequest.Parameters {
    
    static var base: [String: String] {
        
        var parameters: [String : String] = [:]
        parameters["OSbit"] = "64"
        parameters["role"] = "1"
        
        #if canImport(UIKit)
        parameters["platform"] = "3"
        parameters["clientVersion"] = "2.2.6"
        parameters["OSName"] = "macOS.15.1.1"
        parameters["macAddr"] = "1"
        #endif
        
        #if canImport(AppKit)
        parameters["platform"] = "2"
        parameters["clientVersion"] = "2.2.6"
        parameters["OSName"] = "iOS.13.1"
        parameters["macAddr"] = "ac:de:48:00:11:22"
        #endif
        
        return parameters
    }
}
